<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\matrix\controller;
use cmf\controller\PluginMatrixRestBaseController;
use think\Db;
use think\Validate;

/**
 * Class ApiUserController
 * http://192.168.2.219/plugin/matrix/api_user/
 * @package plugins\matrix\controller
 */
class ApiUserController extends PluginMatrixRestBaseController
{
    /**
     * 构造函数
     */
    public function _initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10000, 'msg' => '未登录']);
        }
    }

    public function index()
    {
        //$where['status'] = 1;
        //$where['matrix_code'] = $this->user['matrix_code'];
        $paginate = Db::name('matrix_user')
           // ->where($where)
            ->fieldRaw("id,name,user_name,user_type,department_id,department_name,create_time")
            ->order("create_time DESC")
            ->paginate(10);

        $list = $paginate->items();
        if(count($list) > 0){
            foreach ($list as $key => &$val) {
               if($val['user_type'] == 1){
                   $val['user_type'] = "超级管理员";
               }else{
                   $val['user_type'] = "区域管理员";
               }
                $val['create_time'] = date("Y-m-d h:i",$val['create_time']);
            }
            $this->success('success',array("total_count"=>$paginate->total(),"list"=>$list));
        }else{
            $this->successcode(2,"暂无数据");
        }
    }



    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'name'          => 'require',
                'user_name' => 'require|unique:matrix_user,user_name',
                'user_pass'          => 'require',
                'department_id'          => 'require'
            ]);

            $validate->message([
                'name.require'          => '姓名不能为空!',
                'user_name.require'          => '账号不能为空!',
                'user_name.unique'          => '账号已注册!',
                'user_pass.require'          => '密码不能为空!',
                'department_id.require'          => '部门ID不能为空!'
            ]);
            $data = $this->request->param();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }else{
                $info["name"] = $data["name"];
                $info["user_name"] = $data["user_name"];
                $info["user_pass"] = cmf_password($data["user_pass"]);
                $info["user_type"] = 2;
                $department = DB::name('matrix_department')->where(array("id"=>$data["department_id"]))->find();
                $info["department_id"] = $department["id"];
                $info["department_name"] = $department["name"];
                $info['user_status'] = 1;
                $info['create_time'] = time();
                $result = DB::name('matrix_user')->insertGetId($info);
                if ($result !== false) {
                    $this->success("添加成功!");
                } else {
                    $this->error("添加失败！");
                }
            }
        }else{
            $this->error("参数异常！");
        }
    }


    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'id'          => 'require',
                'name'          => 'require',
                'user_name' => 'require|unique:matrix_user,user_name',
                'user_pass'          => 'require',
                'department_id'          => 'require'
            ]);

            $validate->message([
                'id.require'          => 'ID不能为空!',
                'name.require'          => '姓名不能为空!',
                'user_name.require'          => '账号不能为空!',
                'user_name.unique'          => '账号已注册!',
                'user_pass.require'          => '密码不能为空!',
                'department_id.require'          => '部门ID不能为空!'
            ]);
            $data = $this->request->param();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }else{

                $info["name"] = $data["name"];
                $info["user_name"] = $data["user_name"];
                if(!empty($data["user_pass"])){
                    $info["user_pass"] = cmf_password($data["user_pass"]);
                }
                $info["user_type"] = 2;
                $department = DB::name('matrix_department')->where(array("id"=>$data["department_id"]))->find();
                $info["department_id"] = $department["id"];
                $info["department_name"] = $department["name"];

                $result  = DB::name('matrix_user')->where(array("id"=>$data["id"]))->update($info);
                if ($result !== false) {
                    $this->success("修改成功!");
                } else {
                    $this->error("修改失败！");
                }
            }
        }else{
            $this->error("参数异常！");
        }
    }

    public function delete()
    {
        $validate = new Validate([
            'id'          => 'require'
        ]);
        $validate->message([
            'id.require'          => 'ID不能为空!'
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }else{
            if($data["id"] != 1){
                $result = DB::name('matrix_user')->where(["id" => $data["id"]])->delete();
                if ($result !== false) {
                    $this->success("删除成功!");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("超级管理员不能删除！");
            }
        }
    }

}
